package service.grant.impl;

import java.util.List;

import model.company.Companybasic;
import model.grant.Customgrant;
import model.personal.Personalbasic;

import dao.CompanybasicDao;
import dao.CustomgrantDao;
import dao.PersonalbasicDao;
import exception.Exceptions;
import service.grant.GrantService;

public class GrantServiceImpl implements GrantService {
	private CustomgrantDao grantDao;
	private PersonalbasicDao personalDao;
	private CompanybasicDao companyDao;
	
	public List<?>getCustomList(String type){
		String select="";
		String hql="";
		if(type.equals("1")){
				select="distinct custom.id,custom.name,custom.certificatenumber,custom.name,custom.homephone";
				hql="personalbasic as custom,creditapplypersonal as p where custom.id=p.clientid";
				return personalDao.findSqlQuery(select,hql, 10);
		}else{
		select="distinct custom.id,custom.name,custom.organizationcode,contact.linkman,contact.telephone";
		hql="companybasic as custom,companycontact as contact,creditapplyenterprise as c where custom.id=contact.cid and custom.id=c.clientid";
		return companyDao.findSqlQuery(select,hql, 10);	
		}
	}
	
	public Companybasic getCompanybasic(Integer customid) throws Exceptions{
		Companybasic company=companyDao.findOne("id", customid);
		if(company==null) throw new Exceptions("数据获取异常，数据库中没有此记录");
		return company;
	}
	public Personalbasic getPersonalbasic(Integer customid) throws Exceptions{
		Personalbasic personal=personalDao.findOne("id", customid);
		if(personal==null)throw new Exceptions("数据获取异常，数据库中没有此记录");
		return personal;
	}
	
	public void save_in_customgrant(Customgrant grant){
		grantDao.save(grant);
	}
	public List<?> getGrantList(String custom){
		String select="new vo.ShowGrantBean(grant.id,custom.name,grant.grantmoney,grant.usedmoney)";
		String hql="Customgrant as grant ,"+custom+" as custom where grant.customid=custom.id";
		return grantDao.findQuery(select,hql, 10);
	}
	public Customgrant getGrant(Integer grantid) throws Exceptions{
		Customgrant grant=grantDao.findOne("id", grantid);
		if(grant==null)throw new Exceptions("数据获取异常，数据库中没有此记录");
		return grant;
	}
	public Customgrant getGrant(String name,Integer value) throws Exceptions{
		Customgrant grant=grantDao.findOne(name, value);
		if(grant==null)throw new Exceptions("数据获取异常，数据库中没有此记录");
		return grant; 
	}
	public Customgrant getGrantBycid(Integer cid){
		return grantDao.findOne("customid", cid);
	}
	//------get/set-----//
	public CustomgrantDao getGrantDao() {
		return grantDao;
	}

	public void setGrantDao(CustomgrantDao grantDao) {
		this.grantDao = grantDao;
	}
	public CompanybasicDao getCompanyDao() {
		return companyDao;
	}
	public void setCompanyDao(CompanybasicDao companyDao) {
		this.companyDao = companyDao;
	}

	public PersonalbasicDao getPersonalDao() {
		return personalDao;
	}

	public void setPersonalDao(PersonalbasicDao personalDao) {
		this.personalDao = personalDao;
	}

}
